//创建用户相关的小仓库
import { defineStore } from 'pinia';

//引入登录接口
import { reqLogin ,reqUserInfo,reqLogout} from "@/api/user";
//引入数据类选
import type {loginFormData,loginResponseData,userInfoReponseData} from '@/api/user/type'
// import type { loginFormData, loginResponseData, userInfoReponseData } from "./type";
import type {UserState} from './types/type'
import { SET_TOKEN,GET_TOKEN,REMOVE_TOKEN } from '@/utils/token';
//引入路由
import { constantRoute} from '@/router/routes'
//创建用户小仓库
let useUserStore = defineStore('user',{
    //小仓库存储数据地方
    // state:():UserState =>{
    state:():any =>{
        return{
            // token: localStorage.getItem('TOKEN'),//用户唯一标识token
            token: GET_TOKEN(),//用户唯一标识token
            menuRoutes:constantRoute,//仓库存储的数组路由
            username: '',
            avatar: '',
        }
    },
    //异步|逻辑的地方
    actions:{
        //用户登录的方法
        async userLogin(data:loginFormData){
        // async userLogin(data:any){
            let result:loginResponseData = await reqLogin(data);
            // let result:any = await reqLogin(data);
            if (result.code == 200){
                this.token = (result.data as string);
                // localStorage.setItem('TOKEN',(result.data.token as string))
                SET_TOKEN(result.data)
                //能保证当前async函数返回一个成功的promise
                return 'ok';
            }else{
                return Promise.reject(new Error(result.data));
            }
            
        },
        //获取用户信息
        //获取用户信息方法
        async userInfo() {
            //获取用户信息进行存储仓库当中[用户头像、名字]
            // let result = await reqUserInfo();
            // console.log("result===",result);
            
            let result: userInfoReponseData = await reqUserInfo();
            //如果获取用户信息成功，存储一下用户信息
            if (result.code == 200) {
                this.username = result.data.name;
                this.avatar = result.data.avatar;
                // this.buttons = result.data.buttons;
                //计算当前用户需要展示的异步路由
                // let userAsyncRoute = filterAsyncRoute(cloneDeep(asnycRoute), result.data.routes);
                //菜单需要的数据整理完毕
                // this.menuRoutes = [...constantRoute, ...userAsyncRoute, anyRoute];
                //目前路由器管理的只有常量路由:用户计算完毕异步路由、任意路由动态追加
                // [...userAsyncRoute, anyRoute].forEach((route: any) => {
                //     router.addRoute(route);
                // });
                return 'ok';
            } else {
                return Promise.reject(new Error(result.message));
            }
        },
        //退出登录
        async userLogout() {
            //退出登录请求
            let result: any = await reqLogout();
            if (result.code == 200) {
                //目前没有mock接口:退出登录接口(通知服务器本地用户唯一标识失效)
                this.token = '';
                this.username = '';
                this.avatar = '';
                REMOVE_TOKEN();
                return 'ok';
            } else {
                return Promise.reject(new Error(result.message));
            }
        }
    }
})

//对外暴露获取小仓库方法
export default useUserStore;